﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using ChinaAzureStorageRedirector.Properties;
using System.Drawing;
using Fiddler;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;

namespace ChinaAzureStorageRedirector
{

    class ContextMenus
    {

        bool isHelpLoaded = false;
        bool isRuleLoaded = false;
        bool isEnabled = true;
        private NotifyIcon _ni;
        List<String> allRules;
        public ContextMenuStrip Create(NotifyIcon ni)
        {
            _ni = ni;

            ContextMenuStrip menu = new ContextMenuStrip();
            ToolStripMenuItem item;
            ToolStripSeparator sep;

            // Rules
            item = new ToolStripMenuItem();
            item.Text = "Rules";
            item.Click += new EventHandler(Rules_Click);
            item.Image = Resources.Rules;
            menu.Items.Add(item);

            // Disable
            item = new ToolStripMenuItem();
            item.Text = "Disable";
            item.Click += new EventHandler(Disable_Click);
            item.Image = Resources.Disable;
            menu.Items.Add(item);


            // Help
            item = new ToolStripMenuItem();
            item.Text = "Help";
            item.Click += new EventHandler(Help_Click);
            item.Image = Resources.About;
            menu.Items.Add(item);


            // Separator
            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            // Exit
            item = new ToolStripMenuItem();
            item.Text = "Exit";
            item.Click += new System.EventHandler(Exit_Click);
            item.Image = Resources.Exit;
            menu.Items.Add(item);


            FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
            FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default);
            allRules = Helper.AllRules;
            return menu;
        }

        private void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
        {
            string s = oSession.url;
            if (s.Contains(".core.windows.net"))
            {
                if (allRules.Count > 0)
                {
                    string strAccountString = s.Substring(0, s.IndexOf(".core.windows.net"));

                    foreach (var item in allRules)
                    {
                        if (strAccountString.Contains(item))
                        {
                            oSession.url = s.Replace(".core.windows.net", ".core.chinacloudapi.cn");
                        }
                    }
                }
                else
                {
                    oSession.url = s.Replace(".core.windows.net", ".core.chinacloudapi.cn");
                }
            }
        }

        private void Disable_Click(object sender, EventArgs e)
        {
            if (isEnabled)
            {
                FiddlerApplication.BeforeRequest -= FiddlerApplication_BeforeRequest;
                _ni.ContextMenuStrip.Items[1].Text = "Enable";
                _ni.ContextMenuStrip.Items[1].Image = Resources.Enable;
                _ni.Icon = Resources.StorageRewriterDisabled;
                isEnabled = false;
            }
            else
            {
                FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
                _ni.ContextMenuStrip.Items[1].Text = "Disable";
                _ni.ContextMenuStrip.Items[1].Image = Resources.Disable;
                _ni.Icon = Resources.StorageRewriter;
                isEnabled = true;
            }
        }

        void Rules_Click(object sender, EventArgs e)
        {
            if (!isRuleLoaded)
            {
                isRuleLoaded = true;
                new Rules().ShowDialog();
                allRules = Helper.AllRules;
                isRuleLoaded = false;
            }
        }

        void Help_Click(object sender, EventArgs e)
        {
            if (!isHelpLoaded)
            {
                isHelpLoaded = true;
                new HelpBox().ShowDialog();
                isHelpLoaded = false;
            }
        }

        void Exit_Click(object sender, EventArgs e)
        {
            if (FiddlerApplication.IsStarted())
            {
                FiddlerApplication.Shutdown();
            }

            Application.Exit();
        }
    }
}